package com.task.manager.controller.index;

import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.task.manager.domain.Task;
import com.task.manager.service.home.TaskService;

@Controller
@RequestMapping("/task")
public class TaskController {

	@Autowired
	private TaskService taskService;

	@RequestMapping("/index")
	public String task(Map<String, Object> map) {
		map.put("tasks", taskService.queryTaskByParam(null, null));
		return "task/task";
	}
	
	@PostMapping("/insert")
	public String insert(Task task,Map<String, Object> map) {
		task.setStatus(false);
		task.setCreateTime(new Date());
		task.setUpdateTime(new Date());
		taskService.insertTask(task);
		return "redirect:/home/index?initButton=task"; 
	}
	
	@PostMapping("/update")
	public String update(Task task,Map<String, Object> map) {
		task.setStatus(false);
		task.setCreateTime(new Date());
		task.setUpdateTime(new Date());
		taskService.updateTask(task);
		return "redirect:/home/index?initButton=task"; 
	}
	
	@PostMapping("/delete")
	public String delete(Integer id,Map<String, Object> map) {
		taskService.deleteTask(id);
		return "redirect:/home/index?initButton=task"; 
	}
}
